﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using ASBCC.Database.Entity;
using ASBCC.Database.Service;
using SourceCode.Workflow.Client;

namespace ASBCC.Workflow.K2Manager
{
    public class PostSubmitActionHandler : ActionHandlerBase
    {
        #region Constructor

        public PostSubmitActionHandler(SerializableDictionary dataFields, K2AppForm appForm, K2WorkItem workItem, K2ActionInfo actionInfo, string comment, K2AuditDestination auditDestination, List<K2User> outputDestinationUsers) :
            base(dataFields, appForm, workItem, actionInfo, comment, auditDestination, outputDestinationUsers)
        {
        }

        #endregion

        #region Override Methods

        protected override void SendMail()
        {
            string actionType = GetActionType();
            string ApplicationNameEn = FlowProcess.ProcDisplayEn;
            string ApplicationNameCn = FlowProcess.ProcDisplayCn;

            Flow_ProcessMailConfigTableEntity mailConfig = K2DataHelper.GetProcessMailConfig(WorkItem.ProcName, WorkItem.ActivityName, actionType);
            if (mailConfig == null) return;
            EZVisitApplicationTableEntity entity = EZVisitApplicationManager.SelectByKeys(AppForm.FormId);


            // 通知下一步审批人
            if (NewMyActivityList != null && NewMyActivityList.Count > 0)
            {
                foreach (MyActivityTableEntity myActivity in NewMyActivityList)
                {
                    if (myActivity.OwnerID == K2User.TempUser) continue; //临时用户不需要通知

                    int toAuditorTemplate = mailConfig.ToAuditorTemplate ?? 0;
                    if (toAuditorTemplate <= 0) continue;

                    EmployeeTableEntity ownerInfo = K2DataHelper.GetUserInfoById(myActivity.OwnerID ?? 0);

                    if (!CheckMailApproverRule(ownerInfo)) continue;

                    string[] mailAddressArr = new string[] { ownerInfo.Email };
                    NameValueCollection collection = new NameValueCollection();
                    collection["{EnglishName}"] = myActivity.OwnerEn;
                    collection["{ChineseName}"] = myActivity.OwnerCn;
                    if (!entity.EZVisitReqNoIsNull())
                        collection["{SerialNumber}"] = entity.EZVisitReqNo;
                    else
                        collection["{SerialNumber}"] = myActivity.ApplicationNo;
                    collection["{FormURL}"] = myActivity.FormURL;

                    NameValueCollection titleCollection = new NameValueCollection();
                    if (!entity.EZVisitReqNoIsNull())
                        titleCollection["{SerialNumber}"] = "EZVisit No：" + entity.EZVisitReqNo;
                    else
                        titleCollection["{SerialNumber}"] = myActivity.ApplicationNo;

                    MailService.MailSender(mailAddressArr, toAuditorTemplate, collection, titleCollection);
                }                    
            }
        }

        protected override string GetActionType()
        {
            return K2ActionType.PostSubmit;
        }

        #endregion
    }
}
